﻿using System;
using System.Collections.Generic;
using System.Text;
using DatabaseModule;

namespace dbTestSuite
{
    class Program
    {
        static void Main(string[] args)
        {
            //Connect to the DB
            DAO dbConn = dbFactory.newDB(Properties.Settings.Default.dbType, Properties.Settings.Default.dbConnectionString);
            if (dbConn != null)
            {
                //Set verbose mode
                dbConn.SetVerboseMode(true);

                //Settings
                string tableName = Properties.Settings.Default.dbTable;
                string fieldNameID = Properties.Settings.Default.dbFieldID;
                string fieldNameName = Properties.Settings.Default.dbFieldName;
                string fieldNamePassword = Properties.Settings.Default.dbFieldPassword;

                //Test insertRow()
                for (int i = 1; i <= 3; ++i)
                {
                    Dictionary<string, string> fieldsAndVals = new Dictionary<string, string>();
                    fieldsAndVals.Add(fieldNameID, "adam" + i.ToString());
                    fieldsAndVals.Add(fieldNamePassword, "pass" + i.ToString());
                    fieldsAndVals.Add(fieldNameName, "Adam" + i.ToString());
                    Console.WriteLine(dbConn.InsertRow(tableName, fieldsAndVals));
                    Console.WriteLine(dbConn.GetLastError());
                    Console.Write("\n");
                }

                //Test deleteRows()
                Dictionary<string, string> toDelete = new Dictionary<string, string>();
                toDelete.Add(fieldNameID, "adam3");
                toDelete.Add(fieldNameName, "Adam3");
                Console.WriteLine(dbConn.DeleteRows(tableName, toDelete));
                Console.WriteLine(dbConn.GetLastError());
                Console.Write("\n");

                //Test updateRows();
                Dictionary<string, string> newVals = new Dictionary<string, string>();
                newVals.Add(fieldNamePassword, "THEFORCE");
                newVals.Add(fieldNameName, "Jedi 'Force User' Knight!");
                Dictionary<string, string> keyVals = new Dictionary<string, string>();
                keyVals.Add(fieldNameID, "adam2");
                keyVals.Add(fieldNamePassword, "pass2");
                Console.WriteLine(dbConn.UpdateRows(tableName, newVals, keyVals));
                Console.WriteLine(dbConn.GetLastError());
                Console.Write("\n");

                //Test getRows(), getRow(), and getValue()
                List<Dictionary<string, string>> rows = dbConn.GetRows(tableName, new string[] { fieldNameID, fieldNameName, fieldNamePassword }, null);
                Console.Write(rows.DumpContents() + "\n\n");
                Console.WriteLine(dbConn.GetLastError());
                Console.Write("\n");

                Dictionary<string, string> row = dbConn.GetRow(tableName, new string[] { fieldNameID, fieldNameName, fieldNamePassword }, null);
                Console.Write(row.DumpContents() + "\n\n");
                Console.WriteLine(dbConn.GetLastError());
                Console.Write("\n");

                Console.Write(dbConn.GetValue(tableName, fieldNameID, null) + "\n");
                Console.WriteLine(dbConn.GetLastError());
                Console.Write("\n");
            }
            else
            {
                Console.WriteLine("Error: could not create a db interface instance!");
            }
        }
    }
}
